package es.andresimon.portfolio.web;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.TypedQuery;

import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;

import es.andresimon.portfolio.domain.Picture;
import es.andresimon.portfolio.domain.ProjectPicture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

@RooWebScaffold(path = "projectpictures", formBackingObject = ProjectPicture.class)
@RequestMapping("/projectpictures")
@Controller
public class ProjectPictureController {
	
	public static List<Picture> getPicturesByProjectId(Long projectId) {
		
		if (projectId == null) return null;
		
		List<Picture> pictureList = new ArrayList<Picture>();
		
		TypedQuery<ProjectPicture> projectPictureTypedQuery = ProjectPicture.findProjectPicturesByProjectId(projectId);
		if (projectPictureTypedQuery != null) {
			List<ProjectPicture> projectPictureList = projectPictureTypedQuery.getResultList();
			if (projectPictureList != null) {
				for (ProjectPicture projectPicture : projectPictureList) {
					pictureList.add(PictureController.getPictureByPictureId(projectPicture.getPictureId()));
				}
			}
		} 
		
		return pictureList;
		
	}
}
